package com.pro.controller;

import com.pro.common.GuavaCommon;
import com.pro.pojo.*;
import com.pro.service.ProductService;
import com.pro.utils.GuavaUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService ps;

    @Autowired
    private GuavaUtils guavaUtils;

    //判断权限
    @RequestMapping("/judgePower")
    @ResponseBody
    public JsonData judgePower(Integer id){
        List<Integer> list = ps.judgePower(id);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }
    //获取ztree商品树
    @RequestMapping("/getProductTree")
    @ResponseBody
    public JsonData getProductTree() {
        ProductCategroy productCategroy = new ProductCategroy();
        productCategroy.setCategroyId(0);
        productCategroy.setParentId(-1);
        productCategroy.setTitle("选择分类类型");
        //在缓存中得到这个枚举类，枚举类相当于一个map集合，会以键值对的方式去存储数据，枚举类就是键
        Object ifPresent = guavaUtils.getTenCacheBuild().getIfPresent(String.valueOf(GuavaCommon.ProductCategroyTree));
        //判断这个对象内是否有数据
        if(ifPresent != null){
            return ifPresent != null?JsonData.buildSuccess(ifPresent):JsonData.buildError("服务器出错了");
        }else{
            List<ProductCategroy> list = ps.getProductTree();
            list.add(productCategroy);
            guavaUtils.getTenCacheBuild().put(String.valueOf(GuavaCommon.ProductCategroyTree),list);
        }
        List<ProductCategroy> list = ps.getProductTree();
        list.add(productCategroy);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }
    //添加树节点
    @RequestMapping("/addProductTree")
    @ResponseBody
    public JsonData addProductTree(ProductCategroy productCategroy){
        guavaUtils.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ProductCategroyTree));
        boolean b = ps.addProductCategroy(productCategroy);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }
    //删除树节点
    @RequestMapping("/deleteProductTree")
    @ResponseBody
    public JsonData deleteProductTree(Integer categroyId){
        guavaUtils.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ProductCategroyTree));
        boolean b = ps.deleteProductCategroy(categroyId);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }
    //修改树节点
    @RequestMapping("/updateProductTree")
    @ResponseBody
    public JsonData updateProductTree(String title,Integer categroyId){
        guavaUtils.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ProductCategroyTree));
        boolean b = ps.updateProductCategroy(title, categroyId);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }
    //点击树节点获取信息
    @RequestMapping("/getCheckedProduct")
    @ResponseBody
    public JsonData getCheckedProduct(Integer categroyId){
        List<Product> list = ps.getCheckedProduct(categroyId);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //修改商品状态
    @RequestMapping("/updateProductState")
    @ResponseBody
    public JsonData updateProductState(Integer productSate,Integer productCode) {
        boolean b = ps.updateProductState(productSate, productCode);
        return b!=false?JsonData.buildSuccess(b): JsonData.buildError("服务器出错了");
    }

    //获取表数据
    @RequestMapping("/getProductSearch")
    @ResponseBody
    public JsonData getProductSearch(String productName, String title,Integer productSate,String productLabel) {
        List<Product> productSearch = ps.getProductSearch(productName, title, productSate, productLabel);
        return productSearch!=null?JsonData.buildSuccess(productSearch): JsonData.buildError("服务器出错了");
    }

    //删除表数据
    @RequestMapping("/deleteByProductCodeProduct")
    @ResponseBody
    public JsonData deleteByProductCodeProduct(Integer productCode) {
        boolean b = ps.deleteByProductCodeProduct(productCode);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //高级检索
    @RequestMapping("/getProductRetrieval")
    @ResponseBody
    public JsonData getProductRetrieval(Product product) {
        List<Product> productRetrieval = ps.getProductRetrieval(product);
        return productRetrieval!=null?JsonData.buildSuccess(productRetrieval): JsonData.buildError("服务器出错了");
    }

    //修改表数据
    @RequestMapping("/updateByProductCodeProduct")
    @ResponseBody
    public JsonData updateByProductCodeProduct(Product param) {
        boolean b = ps.updateByProductCodeProduct(param);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //查询所有商品数量
    @RequestMapping("/getProductCounts")
    @ResponseBody
    public JsonData getProductCounts(){
        Integer productCounts = ps.getProductCounts();
        return productCounts != null ? JsonData.buildSuccess(productCounts) : JsonData.buildError("服务器出错了");
    }

    //模糊查询审核表 类型为商品的数据
    @RequestMapping(value = "/getAuditTableAll")
    @ResponseBody
    public JsonData getAuditTableAll(Product product){
        List<Product> auditTableAll = ps.getAuditTableAll(product);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("data",auditTableAll);
        return map.get("data") != null ? JsonData.buildSuccess(map) : JsonData.buildError("服务器出错了");
    }

    //模糊查询审核表 类型为商品的数据
    @RequestMapping(value = "/getRecordResultProducts")
    @ResponseBody
    public JsonData getRecordResultProducts(Product product){
        List<Product> auditTableAll = ps.getRecordResultProducts(product);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("data",auditTableAll);
        return map.get("data") != null ? JsonData.buildSuccess(map) : JsonData.buildError("服务器出错了");
    }

    //添加商品
    @RequestMapping(value = "/addProduct")
    @ResponseBody
    public JsonData addProduct(Product param){
        boolean b = ps.addProduct(param);
        return b? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //添加商品审核
    @RequestMapping(value = "/addCourseRecord")
    @ResponseBody
    public JsonData addCourseRecord(SyCourseRecord syCourseRecord){
        boolean b = ps.addCourseRecord(syCourseRecord);
        return b? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //根据创建人查询个人商品
    @RequestMapping(value = "/getPersonalProducts")
    @ResponseBody
    public JsonData getPersonalProducts(Integer createId){
        List<Product> personalProducts = ps.getPersonalProducts(createId);
        return personalProducts != null ? JsonData.buildSuccess(personalProducts) : JsonData.buildError("服务器出错了");
    }

    //个人商品列表的模糊查
    @RequestMapping(value = "/getPersonalProductsSearch")
    @ResponseBody
    public JsonData getPersonalProductsSearch(String productName, String searchKeywords, String currentpriceProduct){
        List<Product> personalProductsSearch = ps.getPersonalProductsSearch(productName, searchKeywords, currentpriceProduct);
        return personalProductsSearch != null ? JsonData.buildSuccess(personalProductsSearch): JsonData.buildError("服务器出错了");
    }
    //查询所有商品货号
    @RequestMapping(value = "/getProductCode")
    @ResponseBody
    public JsonData getProductCode(){
        List<Product> productCode = ps.getProductCode();
        return productCode != null ? JsonData.buildSuccess(productCode): JsonData.buildError("服务器出错了");
    }
    //根据商品货号查询商品id
    @RequestMapping(value = "/getproductId")
    @ResponseBody
    public JsonData getproductId(Integer productCode){
        Product product = ps.getproductId(productCode);
        return product != null ? JsonData.buildSuccess(product): JsonData.buildError("服务器出错了");
    }

    //删除商品审核记录
    @RequestMapping(value = "/deleteProductRecord")
    @ResponseBody
    public JsonData deleteProductRecord(String courseId){
        boolean b = ps.deleteProductRecord(courseId);
        return b? JsonData.buildSuccess(b): JsonData.buildError("服务器出错了");
    }

    //根据订单id查询商品信息
    @RequestMapping(value = "/getProductByOrderId")
    @ResponseBody
    public JsonData getProductByOrderId(Integer orderId){
        Product productByOrder = ps.getProductByOrderId(orderId);
        return productByOrder != null ? JsonData.buildSuccess(productByOrder) : JsonData.buildError("服务器出错了");
    }
    //根据商品编号查询商品收藏量
    @RequestMapping(value = "/getUserCollection")
    @ResponseBody
    public JsonData getUserCollection(Integer productCode){
        Integer userCollection = ps.getUserCollection(productCode);
        return userCollection != null ? JsonData.buildSuccess(userCollection) : JsonData.buildError("服务器出错了");
    }
    //根据商品编号查询商品收藏量
    @RequestMapping(value = "/getUserOrder")
    @ResponseBody
    public JsonData getUserOrder(Integer productCode){
        Integer userOrder = ps.getUserOrder(productCode);

        return userOrder != null ? JsonData.buildSuccess(userOrder) : JsonData.buildError("服务器出错了");
    }

    //查询商品销量
    @RequestMapping(value = "/getProductBySalesVolume")
    @ResponseBody
    public JsonData getProductBySalesVolume(Integer shopId){
        Product productBySalesVolume = ps.getProductBySalesVolume(shopId);
        return productBySalesVolume != null ? JsonData.buildSuccess(productBySalesVolume) : JsonData.buildError("服务器出错了");
    }

    //商品个人审核详情
    @RequestMapping(value = "/getProductCourseRecord")
    @ResponseBody
    public JsonData getProductCourseRecord(SyCourseRecord syCourseRecord){
        List<SyCourseRecord> productCourseRecord = ps.getProductCourseRecord(syCourseRecord);
        return productCourseRecord != null ? JsonData.buildSuccess(productCourseRecord) : JsonData.buildError("服务器出错了");
    }
    @RequestMapping(value = "/getProductsByNumNamePriceCatgory")
    @ResponseBody
    public JsonData getProductsByNumNamePriceCatgory(Product product){
        List<Product> productsByNumNamePriceCatgory = ps.getProductsByNumNamePriceCatgory(product);
        return productsByNumNamePriceCatgory != null ? JsonData.buildSuccess(productsByNumNamePriceCatgory) : JsonData.buildError("服务器出错了");
    }
    @RequestMapping(value = "/getAllProductUpperCount")
    @ResponseBody
    public JsonData getAllProductUpperCount() {
        int getAllProductUpperCount = ps.getAllProductUpperCount();
        return getAllProductUpperCount != 0 ? JsonData.buildSuccess(getAllProductUpperCount) : JsonData.buildError("0");
    }
}
